$(document).ready(function () {
  $("form").on("submit", function (event) {
    event.preventDefault();

    var id = sessionStorage.getItem(id);
    var token = sessionStorage.getItem(token);

    var oldpassword = $(".oldpassword").val();
    var newpassword = $(".newpassword").val();
    var rnewpassword = $(".rnewpassword").val();

    if (!oldpassword || !newpassword || !rnewpassword) {
      alert("请填写所有必填项");
      return false;
    }
    if (newpassword !== rnewpassword) {
      alert("两次新密码输入不一致");
      return false;
    }

    axios
      .post(
        `http://localhost:9000/users/rpwd`,
        {
          id: sessionStorage.getItem("id"),
          oldPassword: oldpassword,
          newPassword: newpassword,
          rNewPassword: rnewpassword,
        },
        {
          headers: {
            authorization: sessionStorage.getItem("token"),
          },
        }
      )
      .then(function (response) {
        if (response.data.code === 1) {
          alert(response.data.message);
          sessionStorage.clear();
          window.location.href = "./login.html";
        } else {
          alert(response.data.message);
        }
      })
      .catch(function (error) {
        alert("请求出错");
      });
  });
});
